// KSurface.cpp: implementation of the KSurface class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "KSurface.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

KSurface::KSurface(int w, int h) : m_iWidth(w), m_iHeight(h)
{
	CDC* pDC = CDC::FromHandle(::GetDC(::GetDesktopWindow()));
	m_dcMem.CreateCompatibleDC(pDC);
	m_bmpDraw.CreateCompatibleBitmap(pDC, w, h);
	m_dcMem.SelectObject(&m_bmpDraw);
}

KSurface::KSurface()
{
	m_iWidth = 0;
	m_iHeight = 0;
}

KSurface::~KSurface()
{
	m_dcMem.DeleteDC();
	m_bmpDraw.DeleteObject();
}

bool KSurface::Create(int w, int h)
{
	m_dcMem.DeleteDC();
	m_bmpDraw.DeleteObject();

	m_iWidth = w;
	m_iHeight = h;
	CDC* pDC = CDC::FromHandle(::GetDC(::GetDesktopWindow()));
	m_dcMem.CreateCompatibleDC(pDC);
	m_bmpDraw.CreateCompatibleBitmap(pDC, w, h);
	m_dcMem.SelectObject(&m_bmpDraw);

	return true;
}

void KSurface::Fill(COLORREF crColor)
{
	m_dcMem.FillSolidRect(0, 0, GetWidth(), GetHeight(), crColor);
}

void KSurface::SetPixel(int x, int y, COLORREF crColor)
{
	m_dcMem.SetPixel(x, y, crColor);
}

COLORREF KSurface::GetPixel(int x, int y)
{
	return m_dcMem.GetPixel(x, y);
}

void KSurface::MoveTo(int x, int y)
{
	m_dcMem.MoveTo(x, y);
}

void KSurface::LineTo(int x, int y, COLORREF crPen)
{
	CPen pen(PS_SOLID, 1, crPen);
	CPen* pOldPen = m_dcMem.SelectObject(&pen);
	m_dcMem.LineTo(x, y);
	m_dcMem.SelectObject(pOldPen);
}

void KSurface::Display(CDC* pDC)
{
	pDC->BitBlt(0, 0, GetWidth(), GetHeight(), &m_dcMem, 0, 0, SRCCOPY);
}